package com.avic.map.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.avic.map.common.constant.CacheConstant;
import com.avic.map.common.constant.LocalResourceConstant;
import com.avic.map.common.model.MapCommonModel;
import com.avic.map.common.model.SocketCommonModel;
import com.avic.map.common.model.enums.EmergencyStateEnums;
import com.avic.map.common.model.enums.MapDataStatusEnums;
import com.avic.map.common.model.enums.SocketDataTypeEnums;
import com.avic.map.common.util.AssertUtil;
import com.avic.map.common.util.ex.BizErrorCode;
import com.avic.map.common.util.ex.BizException;
import com.avic.map.dal.condition.EmergencyListCondition;
import com.avic.map.dal.model.Emergency;
import com.avic.map.dal.model.EmergencyCar;
import com.avic.map.dal.model.EmergencyFireSign;
import com.avic.map.dal.model.EmergencySoldier;
import com.avic.map.dal.model.EmergencyTagging;
import com.avic.map.service.CarResourceService;
import com.avic.map.service.EmergencyService;
import com.avic.map.service.ShipResourceService;
import com.avic.map.service.SingleSoldierResourceService;
import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.socket.TextMessage;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @Description
 * @Author huangsheng
 * @Date 2018/10/22 11:59
 */
@Service
@Slf4j
public class EmergencyServiceImpl extends AbstractServiceImpl implements EmergencyService {

    /**
     * 地图资源数据service
     */
    @Autowired
    private ShipResourceService shipResourceService;

    /**
     * 地图车辆资源数据service
     */
    @Autowired
    private CarResourceService carResourceService;

    /**
     * 地图单兵资源数据service
     */
    @Autowired
    private SingleSoldierResourceService singleSoldierResourceService;

    @Override
    public void saveMapFireSign(List<EmergencyFireSign> mapFireSigns) {
        try {
            emergencyFireSignMapper.batchInsertSign(mapFireSigns);
        } catch (Exception e) {
            log.error("保存火点标记数据时发生错误 {}", e.getMessage());
        }
    }

    @Override
    public void emergencyStartUpdateMapCarResourceState(List<String> carNumberS) {
        try {
            if (null != carNumberS && carNumberS.size() > 0) {
                for (String carNumber : carNumberS) {
                    log.info("车辆 {} 启动应急", carNumber);
                    if (LocalResourceConstant.CAR_CURRENT_POSITION_MAP.containsKey(carNumber)) {
                        MapCommonModel mapCommonModel = LocalResourceConstant.CAR_CURRENT_POSITION_MAP.get(carNumber);
                        log.info("车辆 {} 此时的状态为 {}", carNumber, mapCommonModel.getStatus());

//                        AssertUtil.isTrue(mapCommonModel.getStatus() == MapDataStatusEnums.SIGN.getType(), BizErrorCode.PARAM_ERROR,
//                                "车辆 【" + carNumber + "】当前不在线不能加入应急");
                        if (null != mapCommonModel && mapCommonModel.getStatus() == MapDataStatusEnums.CAR_ONLINE.getType()) {
                            LocalResourceConstant.CAR_CURRENT_STATE_MAP.put(carNumber, MapDataStatusEnums.CAR_ONLINE_EMERGENCY.getType());
                            mapCommonModel.setStatus(MapDataStatusEnums.CAR_ONLINE_EMERGENCY.getType());
                            LocalResourceConstant.CAR_CURRENT_POSITION_MAP.put(carNumber, mapCommonModel);
                            // 推送消息
                            mapSocketHandler.sendMessageToAllUsers(new TextMessage(JSONObject.toJSONString(
                                    new SocketCommonModel<>(SocketDataTypeEnums.OBJECT_MAP_COMMON_MODEL, mapCommonModel))));

                            carResourceService.updateCarState(MapDataStatusEnums.CAR_ONLINE_EMERGENCY.getType(), carNumber);

                            // 更新redis数据
                            try {
                                redisTemplate.opsForHash().put(
                                        CacheConstant.REDIS_MAP_CURRENT_CAR_SPACE, carNumber, objectMapper.writeValueAsString(mapCommonModel));
                            } catch (JsonProcessingException e) {
                                log.warn("objectMapper转换数据时发生错误{}", e.getMessage());
                            }
                        } else {
                            log.info("车辆 {} 状态不是在线状态，不能加入应急", carNumber);
                        }
                    }
                }
            }
        } catch (
                Exception e) {
            log.error("更新车辆应急数据时发生错误 {}", e.getMessage());
            throw new BizException(BizErrorCode.SYSTEM_ERROR);
        }
    }

    @Override
    public void emergencyStartUpdateMapCarResourceState(String carNumber, int updateState) {
        try {
            log.info("车辆 {} 启动应急", carNumber);
            AssertUtil.isTrue(LocalResourceConstant.CAR_CURRENT_POSITION_MAP.containsKey(carNumber),
                    BizErrorCode.PARAM_ERROR, "编号为 【" + carNumber + "】的车辆不存在");

            MapCommonModel mapCommonModel = LocalResourceConstant.CAR_CURRENT_POSITION_MAP.get(carNumber);
            log.info("车辆 {} 此时的状态为 {}", carNumber, mapCommonModel.getStatus());

            if (updateState == MapDataStatusEnums.CAR_ONLINE_EMERGENCY.getType()) {
                // 添加应急操作
                AssertUtil.isTrue(mapCommonModel.getStatus() == MapDataStatusEnums.CAR_ONLINE.getType(),
                        BizErrorCode.PARAM_ERROR, "编号为 【" + carNumber + "】的车辆当前不在线，不能参加应急");

                LocalResourceConstant.CAR_CURRENT_STATE_MAP.put(carNumber, MapDataStatusEnums.CAR_ONLINE_EMERGENCY.getType());
                mapCommonModel.setStatus(MapDataStatusEnums.CAR_ONLINE_EMERGENCY.getType());
                // 跟新状态
                carResourceService.updateCarState(MapDataStatusEnums.CAR_ONLINE_EMERGENCY.getType(), carNumber);
            } else if (updateState == MapDataStatusEnums.CAR_OFFLINE_EMERGENCY.getType()) {
                // 移除应急
                AssertUtil.isTrue(mapCommonModel.getStatus() == MapDataStatusEnums.CAR_ONLINE_EMERGENCY.getType() ||
                                mapCommonModel.getStatus() == MapDataStatusEnums.CAR_OFFLINE_EMERGENCY.getType(),
                        BizErrorCode.PARAM_ERROR, "车辆当前没有参与应急，不能移除应急");

                if (mapCommonModel.getStatus() == MapDataStatusEnums.CAR_ONLINE_EMERGENCY.getType()) {
                    LocalResourceConstant.CAR_CURRENT_STATE_MAP.put(carNumber, MapDataStatusEnums.CAR_ONLINE.getType());
                    mapCommonModel.setStatus(MapDataStatusEnums.CAR_ONLINE.getType());
                    // 跟新状态
                    carResourceService.updateCarState(MapDataStatusEnums.CAR_ONLINE.getType(), carNumber);
                } else if (mapCommonModel.getStatus() == MapDataStatusEnums.CAR_OFFLINE_EMERGENCY.getType()) {
                    LocalResourceConstant.CAR_CURRENT_STATE_MAP.put(carNumber, MapDataStatusEnums.CAR_OFFLINE.getType());
                    mapCommonModel.setStatus(MapDataStatusEnums.CAR_OFFLINE.getType());
                }
            }

            LocalResourceConstant.CAR_CURRENT_POSITION_MAP.put(carNumber, mapCommonModel);
            // 推送消息
            mapSocketHandler.sendMessageToAllUsers(new TextMessage(JSONObject.toJSONString(
                    new SocketCommonModel<>(SocketDataTypeEnums.OBJECT_MAP_COMMON_MODEL, mapCommonModel))));
            // 更新redis数据
            try {
                redisTemplate.opsForHash().put(
                        CacheConstant.REDIS_MAP_CURRENT_CAR_SPACE, carNumber, objectMapper.writeValueAsString(mapCommonModel));
            } catch (JsonProcessingException e) {
                log.warn("objectMapper转换数据时发生错误{}", e.getMessage());
            }
        } catch (Exception e) {
            log.error("更新车辆应急数据时发生错误 {}", e.getMessage());
            throw new BizException(BizErrorCode.SYSTEM_ERROR);
        }
    }


    @Override
    public void emergencyStartUpdateMapShipResourceState(List<Long> shipNumberS) {
        try {
            if (null != shipNumberS && shipNumberS.size() > 0) {
                for (long shipNumber : shipNumberS) {
                    log.info("船舶 {} 启动应急", shipNumber);
                    if (LocalResourceConstant.SHIP_CURRENT_POSITION_MAP.containsKey(shipNumber)) {
                        MapCommonModel mapCommonModel = LocalResourceConstant.SHIP_CURRENT_POSITION_MAP.get(shipNumber);
                        log.info("船舶 {} 此时的状态为 {}", shipNumber, mapCommonModel.getStatus());

//                        AssertUtil.isTrue(mapCommonModel.getStatus() == MapDataStatusEnums.SIGN.getType(), BizErrorCode.PARAM_ERROR,
//                                "船舶 【" + shipNumber + "】当前不在线不能加入应急");
//
//                        LocalResourceConstant.SHIP_CURRENT_STATE_MAP.put(shipNumber, MapDataStatusEnums.JOIN.getType());
//                        mapCommonModel.setStatus(MapDataStatusEnums.JOIN.getType());
//                        LocalResourceConstant.SHIP_CURRENT_POSITION_MAP.put(shipNumber, mapCommonModel);
//                        // 推送消息
//                        mapSocketHandler.sendMessageToAllUsers(new TextMessage(JSONObject.toJSONString(
//                                new SocketCommonModel<MapCommonModel>(SocketDataTypeEnums.POS_OBJECT, mapCommonModel))));
//                        // 跟新状态
//                        mapShipResourceMapper.updateShipState(MapDataStatusEnums.JOIN.getType(), String.valueOf(shipNumber));
                        // 更新redis数据
                        try {
                            redisTemplate.opsForHash().put(
                                    CacheConstant.REDIS_MAP_CURRENT_SHIP_SPACE, String.valueOf(shipNumber), objectMapper.writeValueAsString(mapCommonModel));
                        } catch (JsonProcessingException e) {
                            log.warn("objectMapper转换数据时发生错误{}", e.getMessage());
                        }
                    }
                }
            }
        } catch (Exception e) {
            log.error("更新船舶应急数据时发生错误 {}", e.getMessage());
            throw new BizException(BizErrorCode.SYSTEM_ERROR);
        }
    }

    @Override
    public void emergencyStartUpdateMapShipResourceState(long shipNumber, int updateState) {
        log.info("船舶 {} 启动应急", shipNumber);
        AssertUtil.isTrue(LocalResourceConstant.SHIP_CURRENT_POSITION_MAP.containsKey(shipNumber),
                BizErrorCode.PARAM_ERROR, "编号为 【" + shipNumber + "】的船舶不存在");

        MapCommonModel mapCommonModel = LocalResourceConstant.SHIP_CURRENT_POSITION_MAP.get(shipNumber);
        log.info("船舶 {} 此时的状态为 {}", shipNumber, mapCommonModel.getStatus());

//        if (updateState == MapDataStatusEnums.JOIN.getType()) {
//            // 添加应急操作
//            AssertUtil.isTrue(mapCommonModel.getStatus() == MapDataStatusEnums.SIGN.getType(),
//                    BizErrorCode.PARAM_ERROR, "船舶当前不在线不能加入应急");
//            LocalResourceConstant.SHIP_CURRENT_STATE_MAP.put(shipNumber, MapDataStatusEnums.JOIN.getType());
//            mapCommonModel.setStatus(MapDataStatusEnums.JOIN.getType());
//            // 跟新状态
//            mapShipResourceMapper.updateShipState(MapDataStatusEnums.JOIN.getType(), String.valueOf(shipNumber));
//        } else if (updateState == MapDataStatusEnums.SIGN.getType()) {
//            // 移除应急
//            // 添加应急操作
//            AssertUtil.isTrue(mapCommonModel.getStatus() == MapDataStatusEnums.JOIN.getType(),
//                    BizErrorCode.PARAM_ERROR, "船舶当前不应急操作不能移除应急");
//            LocalResourceConstant.SHIP_CURRENT_STATE_MAP.put(shipNumber, MapDataStatusEnums.SIGN.getType());
//            mapCommonModel.setStatus(MapDataStatusEnums.SIGN.getType());
//            // 跟新状态
//            mapShipResourceMapper.updateShipState(MapDataStatusEnums.SIGN.getType(), String.valueOf(shipNumber));
//        }
//
//        LocalResourceConstant.SHIP_CURRENT_POSITION_MAP.put(shipNumber, mapCommonModel);
//        // 推送消息
//        mapSocketHandler.sendMessageToAllUsers(new TextMessage(JSONObject.toJSONString(
//                new SocketCommonModel<MapCommonModel>(SocketDataTypeEnums.POS_OBJECT, mapCommonModel))));
        // 更新redis数据
        try {
            redisTemplate.opsForHash().put(
                    CacheConstant.REDIS_MAP_CURRENT_SHIP_SPACE, String.valueOf(shipNumber), objectMapper.writeValueAsString(mapCommonModel));
        } catch (JsonProcessingException e) {
            log.warn("objectMapper转换数据时发生错误{}", e.getMessage());
        }
    }

    @Override
    public void emergencyStartUpdateMapSoldierResourceState(String soldierNumber, int updateState) {
        try {
            log.info("单兵 {} 启动应急", soldierNumber);
            AssertUtil.isTrue(LocalResourceConstant.SOLDIER_CURRENT_POSITION_MAP.containsKey(soldierNumber),
                    BizErrorCode.PARAM_ERROR, "编号为 【" + soldierNumber + "】的单兵不存在");

            MapCommonModel mapCommonModel = LocalResourceConstant.SOLDIER_CURRENT_POSITION_MAP.get(soldierNumber);
            log.info("单兵 {} 此时的状态为 {}", soldierNumber, mapCommonModel.getStatus());

            if (updateState == MapDataStatusEnums.G_SOLDIER_ONLINE_EMERGENCY.getType()) {
                // 添加应急操作
                AssertUtil.isTrue(mapCommonModel.getStatus() == MapDataStatusEnums.G_TALKIE_SOLDIER_ONLINE.getType() ||
                                mapCommonModel.getStatus() == MapDataStatusEnums.WALKIE_TALKIE_SOLDIER_ONLINE.getType(),
                        BizErrorCode.PARAM_ERROR, "编号为 【" + mapCommonModel + "】的单兵当前不在线，不能参加应急");

                if (mapCommonModel.getStatus() == MapDataStatusEnums.G_TALKIE_SOLDIER_ONLINE.getType()) {
                    LocalResourceConstant.SOLDIER_CURRENT_STATE_MAP.put(soldierNumber, MapDataStatusEnums.G_SOLDIER_ONLINE_EMERGENCY.getType());
                    mapCommonModel.setStatus(MapDataStatusEnums.G_SOLDIER_ONLINE_EMERGENCY.getType());
                    // 更新状态
                    singleSoldierResourceService.updateSoldierState(soldierNumber, MapDataStatusEnums.G_SOLDIER_ONLINE_EMERGENCY.getType());
                } else if (mapCommonModel.getStatus() == MapDataStatusEnums.WALKIE_TALKIE_SOLDIER_ONLINE.getType()) {
                    LocalResourceConstant.SOLDIER_CURRENT_STATE_MAP.put(soldierNumber, MapDataStatusEnums.WALKIE_TALKIE_SOLDIER_ONLINE_EMERGENCY.getType());
                    mapCommonModel.setStatus(MapDataStatusEnums.WALKIE_TALKIE_SOLDIER_ONLINE_EMERGENCY.getType());
                    singleSoldierResourceService.updateSoldierState(soldierNumber, MapDataStatusEnums.WALKIE_TALKIE_SOLDIER_ONLINE_EMERGENCY.getType());
                }
            } else if (updateState == MapDataStatusEnums.G_SOLDIER_OFFLINE_EMERGENCY.getType()) {
                // 移除应急
                AssertUtil.isTrue(mapCommonModel.getStatus() == MapDataStatusEnums.G_SOLDIER_ONLINE_EMERGENCY.getType() ||
                                mapCommonModel.getStatus() == MapDataStatusEnums.G_SOLDIER_OFFLINE_EMERGENCY.getType() ||
                                mapCommonModel.getStatus() == MapDataStatusEnums.WALKIE_TALKIE_SOLDIER_ONLINE_EMERGENCY.getType() ||
                                mapCommonModel.getStatus() == MapDataStatusEnums.WALKIE_TALKIE_SOLDIER_OFFLINE_EMERGENCY.getType(),
                        BizErrorCode.PARAM_ERROR, "单兵当前不在应急状态中，不能移除应急");

                if (mapCommonModel.getStatus() == MapDataStatusEnums.G_SOLDIER_ONLINE_EMERGENCY.getType()) {
                    LocalResourceConstant.SOLDIER_CURRENT_STATE_MAP.put(soldierNumber, MapDataStatusEnums.G_TALKIE_SOLDIER_ONLINE.getType());
                    mapCommonModel.setStatus(MapDataStatusEnums.G_TALKIE_SOLDIER_ONLINE.getType());
                    // 更新状态
                    singleSoldierResourceService.updateSoldierState(soldierNumber, MapDataStatusEnums.G_TALKIE_SOLDIER_ONLINE.getType());
                } else if (mapCommonModel.getStatus() == MapDataStatusEnums.G_SOLDIER_OFFLINE_EMERGENCY.getType()) {
                    LocalResourceConstant.SOLDIER_CURRENT_STATE_MAP.put(soldierNumber, MapDataStatusEnums.G_TALKIE_SOLDIER_OFFLINE.getType());
                    mapCommonModel.setStatus(MapDataStatusEnums.G_TALKIE_SOLDIER_OFFLINE.getType());
                    singleSoldierResourceService.updateSoldierState(soldierNumber, MapDataStatusEnums.G_TALKIE_SOLDIER_OFFLINE.getType());
                } else if (mapCommonModel.getStatus() == MapDataStatusEnums.WALKIE_TALKIE_SOLDIER_ONLINE_EMERGENCY.getType()) {
                    LocalResourceConstant.SOLDIER_CURRENT_STATE_MAP.put(soldierNumber, MapDataStatusEnums.WALKIE_TALKIE_SOLDIER_ONLINE.getType());
                    mapCommonModel.setStatus(MapDataStatusEnums.WALKIE_TALKIE_SOLDIER_ONLINE.getType());
                    singleSoldierResourceService.updateSoldierState(soldierNumber, MapDataStatusEnums.WALKIE_TALKIE_SOLDIER_ONLINE.getType());
                } else if (mapCommonModel.getStatus() == MapDataStatusEnums.WALKIE_TALKIE_SOLDIER_OFFLINE_EMERGENCY.getType()) {
                    LocalResourceConstant.SOLDIER_CURRENT_STATE_MAP.put(soldierNumber, MapDataStatusEnums.WALKIE_TALKIE_SOLDIER_OFFLINE.getType());
                    mapCommonModel.setStatus(MapDataStatusEnums.WALKIE_TALKIE_SOLDIER_OFFLINE.getType());
                    singleSoldierResourceService.updateSoldierState(soldierNumber, MapDataStatusEnums.WALKIE_TALKIE_SOLDIER_OFFLINE.getType());
                }
            }

            LocalResourceConstant.SOLDIER_CURRENT_POSITION_MAP.put(soldierNumber, mapCommonModel);
            // 推送消息
            mapSocketHandler.sendMessageToAllUsers(new TextMessage(JSONObject.toJSONString(
                    new SocketCommonModel<>(SocketDataTypeEnums.OBJECT_MAP_COMMON_MODEL, mapCommonModel))));
            // 更新redis数据
            try {
                redisTemplate.opsForHash().put(
                        CacheConstant.REDIS_MAP_CURRENT_SOLDIER_SPACE, soldierNumber, objectMapper.writeValueAsString(mapCommonModel));
            } catch (JsonProcessingException e) {
                log.warn("objectMapper转换数据时发生错误{}", e.getMessage());
            }
        } catch (Exception e) {
            log.error("更新车辆应急数据时发生错误 {}", e.getMessage());
            throw new BizException(BizErrorCode.SYSTEM_ERROR);
        }
    }

    @Override
    public void emergencyStartUpdateMapSoldierResourceState(List<String> soldierNumberS) {
        try {
            if (null != soldierNumberS && soldierNumberS.size() > 0) {
                for (String soldierNumber : soldierNumberS) {
                    log.info("单兵 {} 启动应急", soldierNumber);
                    if (LocalResourceConstant.SOLDIER_CURRENT_POSITION_MAP.containsKey(soldierNumber)) {
                        MapCommonModel mapCommonModel = LocalResourceConstant.SOLDIER_CURRENT_POSITION_MAP.get(soldierNumber);
                        log.info("单兵 {} 此时的状态为 {}", soldierNumber, mapCommonModel.getStatus());

//                        AssertUtil.isTrue(mapCommonModel.getStatus() == MapDataStatusEnums.SIGN.getType(), BizErrorCode.PARAM_ERROR,
//                                "单兵【" + soldierNumber + "】当前不在线不能加入应急");

                        if (null != mapCommonModel) {

                            if (mapCommonModel.getStatus() == MapDataStatusEnums.G_TALKIE_SOLDIER_ONLINE.getType() ||
                                    mapCommonModel.getStatus() == MapDataStatusEnums.WALKIE_TALKIE_SOLDIER_ONLINE.getType()) {

                                int updateState = MapDataStatusEnums.WALKIE_TALKIE_SOLDIER_ONLINE_EMERGENCY.getType();
                                if (mapCommonModel.getStatus() == MapDataStatusEnums.G_TALKIE_SOLDIER_ONLINE.getType()) {
                                    updateState = MapDataStatusEnums.G_SOLDIER_ONLINE_EMERGENCY.getType();
                                }
                                LocalResourceConstant.SOLDIER_CURRENT_STATE_MAP.put(soldierNumber, updateState);
                                mapCommonModel.setStatus(updateState);
                                LocalResourceConstant.SOLDIER_CURRENT_POSITION_MAP.put(soldierNumber, mapCommonModel);
                                // 推送消息
                                mapSocketHandler.sendMessageToAllUsers(new TextMessage(JSONObject.toJSONString(
                                        new SocketCommonModel<>(SocketDataTypeEnums.OBJECT_MAP_COMMON_MODEL, mapCommonModel))));
                                // 更新单兵船舶数据
                                singleSoldierResourceService.updateSoldierState(soldierNumber, updateState);

                                // 更新redis数据
                                try {
                                    redisTemplate.opsForHash().put(
                                            CacheConstant.REDIS_MAP_CURRENT_SOLDIER_SPACE, soldierNumber, objectMapper.writeValueAsString(mapCommonModel));
                                } catch (JsonProcessingException e) {
                                    log.warn("objectMapper转换数据时发生错误{}", e.getMessage());
                                }
                            }
                        } else {
                            log.info("单兵 {} 状态不是在线状态，不能加入应急", soldierNumber);
                        }
                    }
                }
            }
        } catch (Exception e) {
            log.error("更新船舶应急数据时发生错误 {}", e.getMessage());
            throw new BizException(BizErrorCode.SYSTEM_ERROR);
        }
    }

    @Override
    public String createEmergency(String userId) {
        try {
            String itemData = UUID.randomUUID().toString();
            log.info("新建应急 {}", itemData);
            Emergency emergency = new Emergency();
            emergency.setId(itemData);
            emergency.setCreateUserId(userId);
            emergency.setEmergencyState(EmergencyStateEnums.JOIN.getState());
            int insertEmergency = emergencyMapper.insertEmergency(emergency);
            log.info("新增应急结果 {}", insertEmergency);
            return itemData;
        } catch (Exception e) {
            log.error("创建应急数据时发生错误 {}", e.getMessage());
            throw new BizException(BizErrorCode.SYSTEM_ERROR);
        }
    }

    @Override
    @Transactional
    public void endEmergency(String emergencyId, String emergencyEndName) {
        try {
            log.info("更新应急数据");
            Emergency emergency = new Emergency();
            emergency.setEmergencyState(EmergencyStateEnums.END.getState());
            emergency.setEmergencyName(emergencyEndName);
            emergency.setId(emergencyId);
            // 更新应急基础数据
            emergencyMapper.endEmergencyUpdate(emergency);
            // 更新车辆应急数据
            List<String> carList = this.updateCarEmergencyEnd();
            List<String> soldierList = this.updateSoldierEmergencyEnd();
            if (null != carList && carList.size() > 0) {
                emergencyMapper.insertEmergencyCar(carList, emergencyId);
            }
            if (null != soldierList && soldierList.size() > 0) {
                emergencyMapper.insertEmergencySoldier(soldierList, emergencyId);
            }
            emergencyMapper.insertEmergencyTagging(emergencyId);
        } catch (Exception e) {
            log.error("结束应急，更新数据时发生错误 {}", e.getMessage());
            throw new BizException(BizErrorCode.SYSTEM_ERROR);
        }
    }

    private List<String> updateSoldierEmergencyEnd() {

        List<String> soldierNumbers = new ArrayList<>();
        // 处理应急单兵
        Map<String, MapCommonModel> soldierMap = new HashMap<>();
        soldierMap.putAll(LocalResourceConstant.SOLDIER_CURRENT_POSITION_MAP);

        soldierMap.forEach((soldierNumber, mapCommonModel) -> {
            if (mapCommonModel.getStatus() == MapDataStatusEnums.WALKIE_TALKIE_SOLDIER_ONLINE_EMERGENCY.getType() ||
                    mapCommonModel.getStatus() == MapDataStatusEnums.WALKIE_TALKIE_SOLDIER_OFFLINE_EMERGENCY.getType() ||
                    mapCommonModel.getStatus() == MapDataStatusEnums.G_SOLDIER_ONLINE_EMERGENCY.getType() ||
                    mapCommonModel.getStatus() == MapDataStatusEnums.G_SOLDIER_OFFLINE_EMERGENCY.getType()) {
                try {
                    log.info("单兵 {} 接触应急状态", objectMapper.writeValueAsString(mapCommonModel));
                } catch (JsonProcessingException e) {
                }
                soldierNumbers.add(soldierNumber);

                if (mapCommonModel.getStatus() == MapDataStatusEnums.G_SOLDIER_ONLINE_EMERGENCY.getType()) {
                    LocalResourceConstant.SOLDIER_CURRENT_STATE_MAP.put(soldierNumber, MapDataStatusEnums.G_TALKIE_SOLDIER_ONLINE.getType());
                    mapCommonModel.setStatus(MapDataStatusEnums.G_TALKIE_SOLDIER_ONLINE.getType());
                    // 更新状态
                    singleSoldierResourceService.updateSoldierState(soldierNumber, MapDataStatusEnums.G_TALKIE_SOLDIER_ONLINE.getType());
                } else if (mapCommonModel.getStatus() == MapDataStatusEnums.G_SOLDIER_OFFLINE_EMERGENCY.getType()) {
                    LocalResourceConstant.SOLDIER_CURRENT_STATE_MAP.put(soldierNumber, MapDataStatusEnums.G_TALKIE_SOLDIER_OFFLINE.getType());
                    mapCommonModel.setStatus(MapDataStatusEnums.G_TALKIE_SOLDIER_OFFLINE.getType());
                    singleSoldierResourceService.updateSoldierState(soldierNumber, MapDataStatusEnums.G_TALKIE_SOLDIER_OFFLINE.getType());
                } else if (mapCommonModel.getStatus() == MapDataStatusEnums.WALKIE_TALKIE_SOLDIER_ONLINE_EMERGENCY.getType()) {
                    LocalResourceConstant.SOLDIER_CURRENT_STATE_MAP.put(soldierNumber, MapDataStatusEnums.WALKIE_TALKIE_SOLDIER_ONLINE.getType());
                    mapCommonModel.setStatus(MapDataStatusEnums.WALKIE_TALKIE_SOLDIER_ONLINE.getType());
                    singleSoldierResourceService.updateSoldierState(soldierNumber, MapDataStatusEnums.WALKIE_TALKIE_SOLDIER_ONLINE.getType());
                } else if (mapCommonModel.getStatus() == MapDataStatusEnums.WALKIE_TALKIE_SOLDIER_OFFLINE_EMERGENCY.getType()) {
                    LocalResourceConstant.SOLDIER_CURRENT_STATE_MAP.put(soldierNumber, MapDataStatusEnums.WALKIE_TALKIE_SOLDIER_OFFLINE.getType());
                    mapCommonModel.setStatus(MapDataStatusEnums.WALKIE_TALKIE_SOLDIER_OFFLINE.getType());
                    singleSoldierResourceService.updateSoldierState(soldierNumber, MapDataStatusEnums.WALKIE_TALKIE_SOLDIER_OFFLINE.getType());
                }

                // 推送消息
                mapSocketHandler.sendMessageToAllUsers(new TextMessage(JSONObject.toJSONString(
                        new SocketCommonModel<>(SocketDataTypeEnums.OBJECT_MAP_COMMON_MODEL, mapCommonModel))));
                try {
                    // 更新redis数据
                    redisTemplate.opsForHash().put(
                            CacheConstant.REDIS_MAP_CURRENT_SOLDIER_SPACE, soldierNumber, objectMapper.writeValueAsString(mapCommonModel));
                } catch (JsonProcessingException e) {
                    log.warn("objectMapper转换数据时发生错误{}", e.getMessage());
                }
            }
        });
        return soldierNumbers;
    }

    private List<String> updateCarEmergencyEnd() {
        List<String> carNumbers = new ArrayList<>();
        // 处理应急车辆
        Map<String, MapCommonModel> carMap = new HashMap<>();
        carMap.putAll(LocalResourceConstant.CAR_CURRENT_POSITION_MAP);

        carMap.forEach((carNumber, mapCommonModel) -> {
            if (mapCommonModel.getStatus() == MapDataStatusEnums.CAR_ONLINE_EMERGENCY.getType()
                    || mapCommonModel.getStatus() == MapDataStatusEnums.CAR_OFFLINE_EMERGENCY.getType()) {
                try {
                    log.info("车辆 {} 解除应急状态", objectMapper.writeValueAsString(mapCommonModel));
                } catch (JsonProcessingException e) {
                }
                carNumbers.add(carNumber);
                if (mapCommonModel.getStatus() == MapDataStatusEnums.CAR_ONLINE_EMERGENCY.getType()) {
                    log.info("应急车辆 {} 恢复在线", carNumber);
                    LocalResourceConstant.CAR_CURRENT_STATE_MAP.put(carNumber, MapDataStatusEnums.CAR_ONLINE.getType());

                    mapCommonModel.setStatus(MapDataStatusEnums.CAR_ONLINE.getType());
                    LocalResourceConstant.CAR_CURRENT_POSITION_MAP.put(carNumber, mapCommonModel);

                    // 更新DB数据
                    carResourceService.updateCarState(MapDataStatusEnums.CAR_ONLINE.getType(), carNumber);
                } else if (mapCommonModel.getStatus() == MapDataStatusEnums.CAR_OFFLINE_EMERGENCY.getType()) {
                    log.info("应急车辆 {} 恢复离线", carNumber);
                    LocalResourceConstant.CAR_CURRENT_STATE_MAP.put(carNumber, MapDataStatusEnums.CAR_OFFLINE.getType());

                    mapCommonModel.setStatus(MapDataStatusEnums.CAR_OFFLINE.getType());
                    LocalResourceConstant.CAR_CURRENT_POSITION_MAP.put(carNumber, mapCommonModel);

                    // 更新DB数据
                    carResourceService.updateCarState(MapDataStatusEnums.CAR_OFFLINE.getType(), carNumber);
                }
                // 推送消息
                mapSocketHandler.sendMessageToAllUsers(new TextMessage(JSONObject.toJSONString(
                        new SocketCommonModel<>(SocketDataTypeEnums.OBJECT_MAP_COMMON_MODEL, mapCommonModel))));
                try {
                    // 更新redis数据
                    redisTemplate.opsForHash().put(
                            CacheConstant.REDIS_MAP_CURRENT_CAR_SPACE, carNumber, objectMapper.writeValueAsString(mapCommonModel));
                } catch (JsonProcessingException e) {
                    log.warn("objectMapper转换数据时发生错误{}", e.getMessage());
                }
            }
        });
        return carNumbers;
    }

    @Override
    public List<Emergency> getEmergencyList(EmergencyListCondition emergencyListCondition) {
        return emergencyMapper.queryEmergencyList(emergencyListCondition);
    }

    @Override
    public int getEmergencyListTotal(EmergencyListCondition emergencyListCondition) {
        return emergencyMapper.queryEmergencyListTotal(emergencyListCondition);
    }

    @Override
    public List<EmergencyFireSign> getEmergencyFireSignList(String emergencyNumber) {
        return emergencyFireSignMapper.queryEmergencyFireSignList(emergencyNumber);
    }

    @Override
    public Emergency getCurrentEmergency(int emergencyState) {
        return emergencyMapper.queryCurrentEmergency(emergencyState);
    }

    @Override
    public void updateEmergency(Emergency emergency) {
        try {
            emergencyMapper.updateEmergency(emergency);
        } catch (Exception e) {
            log.error("更新应急记录数据时发生错误：{}", e.getMessage());
            throw new BizException(BizErrorCode.SYSTEM_ERROR, "更新应急记录数据时发生错误");
        }
    }

    @Override
    public List<EmergencyCar> emergencyCarList(String emergencyId) {
        return emergencyMapper.emergencyCarList(emergencyId);
    }

    @Override
    public Emergency queryEmergencyDetail(String emergencyId) {
        return emergencyMapper.queryEmergencyDetail(emergencyId);
    }

    @Override
    public List<EmergencySoldier> emergencySoldierList(String emergencyId) {
        return emergencyMapper.emergencySoldierList(emergencyId);
    }

    @Override
    public List<EmergencyTagging> emergencyTaggingList(String emergencyId) {
        return emergencyMapper.emergencyTaggingList(emergencyId);
    }
}
